﻿@namespace MudBlazor
@using MudBlazor.Internal
@using MudBlazor.Resources
@typeparam T
@inherits MudBaseInput<T>
@inject InternalMudLocalizer Localizer

<div class="@Classname" style="@Style">
    @if (Adornment == Adornment.Start)
    {
        <MudInputAdornment Class="@AdornmentClassname"
                Icon="@AdornmentIcon"
                Color="@AdornmentColor"
                Size="@IconSize"
                Text="@AdornmentText"
                Edge="@Edge.Start"
                AdornmentClick="@OnAdornmentClick"
                AriaLabel="@AdornmentAriaLabel"
        />
    }

    @if (AutoGrow || Lines > 1)
    {
        <textarea class="@InputClassname"
                @ref="ElementReference"
                rows="@Lines"
                @attributes="UserAttributes"
                id="@InputElementId"
                type="@InputTypeString"
                placeholder="@Placeholder"
                disabled=@GetDisabledState()
                readonly="@GetReadOnlyState()"
                inputmode="@InputMode.ToDescriptionString()"
                @oninput="OnInput"
                @onchange="OnChange"
                @onblur="@OnBlurredAsync"
                @onkeydown="@InvokeKeyDownAsync"
                @onkeyup="@InvokeKeyUpAsync"
                @onpaste="@OnPaste"
                value="@_internalText"
                maxlength="@MaxLength"
                @onkeydown:preventDefault="@KeyDownPreventDefault"
                @onkeyup:preventDefault="@KeyUpPreventDefault"
                @onmousewheel="@OnMouseWheel"
                @onwheel="@OnMouseWheel"
                aria-describedby="@GetAriaDescribedByString()"
                aria-invalid="@Error.ToString().ToLowerInvariant()"
                required="@Required"
                aria-required="@Required.ToString().ToLowerInvariant()">
            @Text
        </textarea>
        @*Note: double mouse wheel handlers needed for Firefox because it doesn't know onmousewheel*@
        @*note: the value="@_internalText" is absolutely essential here. the inner html @Text is needed by tests checking it*@
    }
    else
    {
        <input class="@InputClassname"
                @ref="ElementReference"
                @attributes="UserAttributes"
                id="@InputElementId"
                type="@InputTypeString"
                value="@_internalText"
                @oninput="OnInput"
                @onchange="OnChange"
                placeholder="@Placeholder"
                disabled=@GetDisabledState()
                readonly="@GetReadOnlyState()"
                @onblur="@OnBlurredAsync"
                inputmode="@InputMode.ToDescriptionString()"
                pattern="@Pattern"
                @onkeydown="@InvokeKeyDownAsync"
                @onkeyup="@InvokeKeyUpAsync"
                maxlength="@MaxLength"
                @onkeydown:preventDefault="KeyDownPreventDefault"
                @onkeyup:preventDefault="@KeyUpPreventDefault"
                @onmousewheel="@OnMouseWheel"
                @onwheel="@OnMouseWheel"
                aria-describedby="@GetAriaDescribedByString()"
                aria-invalid="@Error.ToString().ToLowerInvariant()"
                required="@Required"
                aria-required="@Required.ToString().ToLowerInvariant()">

        @*Note: double mouse wheel handlers needed for Firefox because it doesn't know onmousewheel*@

         @if (GetDisabledState()) {
             @*Note: this div must always be there to avoid crashes in WASM, but it is hidden most of the time except if ChildContent should be shown.
                           In Disabled state the tabindex attribute must NOT be set at all or else it will get focus on click
             *@
            <div class="@InputClassname"
                style="@("display:"+(InputType == InputType.Hidden && ChildContent != null ? "inline" : "none"))"
                @onblur="@OnBlurredAsync" @ref="@_elementReference1"
            >
                @ChildContent
            </div>
        }
        else
        {
             @*Note: this div must always be there to avoid crashes in WASM, but it is hidden most of the time except if ChildContent should be shown.*@
            <div class="@InputClassname"
                 style="@("display:"+(InputType == InputType.Hidden && ChildContent != null ? "inline" : "none"))"
                 tabindex="@(InputType == InputType.Hidden && ChildContent != null ? 0 : -1)"
                 @onblur="@OnBlurredAsync" @ref="@_elementReference1"
            >
                @ChildContent
            </div>
        }
    }

    @if (GetClearable() && !GetDisabledState())
    {
        <MudIconButton Class="@ClearButtonClassname"
                    Color="@Color.Default"
                    Icon="@ClearIcon"
                    Size="@Size.Small"
                    OnClick="@ClearButtonClickHandlerAsync"
                    aria-label="@Localizer[LanguageResource.MudInput_Clear]"
                    tabindex="-1"
                />
    }

    @if (Adornment == Adornment.End)
    {
        <MudInputAdornment Class="@AdornmentClassname"
                Icon="@AdornmentIcon"
                Color="@AdornmentColor"
                Size="@IconSize"
                Text="@AdornmentText"
                Edge="@Edge.End"
                AdornmentClick="@OnAdornmentClick"
                AriaLabel="@AdornmentAriaLabel"
        />
    }

    @if (Variant == Variant.Outlined)
    {
        <div class="mud-input-outlined-border"></div>
    }

    @if (!HideSpinButtons)
    {
        <div class="mud-input-numeric-spin">
            <MudButton Variant="Variant.Text"
                       @onclick="OnIncrement"
                       Disabled="@(GetDisabledState() || GetReadOnlyState())"
                       tabindex="-1"
                       aria-label="@Localizer[LanguageResource.MudInput_Increment]">
                <MudIcon Icon="@NumericUpIcon" Size="@GetButtonSize()" />
            </MudButton>

            <MudButton Variant="Variant.Text"
                       @onclick="OnDecrement"
                       Disabled="@(GetDisabledState() || GetReadOnlyState())"
                       tabindex="-1"
                       aria-label="@Localizer[LanguageResource.MudInput_Decrement]">
                <MudIcon Icon="@NumericDownIcon" Size="@GetButtonSize()" />
            </MudButton>
        </div>
    }
</div>